package com.panopset.sf.menu;

import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.panopset.compat.Strings;
import com.panopset.nls.MsgProps;
import com.panopset.sf.FontManager;
import com.panopset.sf.MnemonicGroup;
import com.panopset.sf.Windoc;

/**
 * Abstract SwingFrame menu.
 * 
 * @author Karl Dinwiddie
 * 
 */
public abstract class AbstractMenu {

	/**
	 * Populate menu with menu items.
	 * 
	 * @param menu
	 *            Menu.
	 */
	protected abstract void populateMenu(JMenu newMenu);

	/**
	 * SwingMenuBar
	 */
	protected final SwingMenuBar sm;

	/**
	 * SwingDoc
	 */
	protected final Windoc sd;

	/**
	 * Title.
	 */
	protected final String title;

	/**
	 * MnemonicGroup.
	 */
	private final MnemonicGroup mg = new MnemonicGroup();

	/**
	 * 
	 * @param swingDoc
	 *            SwingDoc
	 */
	public AbstractMenu(final Windoc swingDoc, final String menuTitle) {
		sd = swingDoc;
		sm = sd.getSwingMenu();
		title = menuTitle;
	}

	private JMenu menu;

	public JMenu getMenu() {
		if (menu == null) {
			menu = new JMenu(title);
			FontManager.getInstance().registerMenuComponent(menu);
			if (!isMnemonicSuppressed) {
				int menuMnemonic = sm.getMenuBarMnemonicGroup().findMnemonic(
						menu);
				mg.add(menuMnemonic);
			}
			populateMenu(menu);
		}
		return menu;
	}

	/**
	 * @param path
	 *            NLS props path.
	 * @param key
	 *            NLS key.
	 * @param al
	 *            Action listener.
	 * @param mg
	 *            Mnemonic group.
	 * @return menu item.
	 */
	protected JMenuItem createMenuItem(final String path, final String key,

	final ActionListener al) {
		return createMenuItem(path, key, al, KeyEvent.VK_UNDEFINED);
	}

	/**
	 * @param path
	 *            NLS props path.
	 * @param key
	 *            NLS key.
	 * @param al
	 *            Action listener.
	 * @param mg
	 *            Mnemonic group.
	 * @param preferredMnemonic
	 *            If this mnemonic is available and is one of the characters in
	 *            the key, it will be used instead of the last one found.
	 * @return menu item.
	 */
	protected JMenuItem createMenuItem(final String path, final String key,

	final ActionListener al, int preferredMnemonic) {
		return createMenuItem(
				Strings.capitalize(new MsgProps(path).get(key)), al,
				preferredMnemonic);
	}

	protected JMenuItem createMenuItem(final String name,

	final ActionListener al, int preferredMnemonic) {
		JMenuItem mi = new JMenuItem(name);
		FontManager.getInstance().registerMenuComponent(mi);
		mi.addActionListener(al);
		mg.findMnemonic(mi, preferredMnemonic);
		return mi;
	}

	protected JMenuItem createMenuItem(String name,
			ActionListener actionListener) {
		return createMenuItem(name, actionListener, KeyEvent.VK_UNDEFINED);
	}

	private boolean isMnemonicSuppressed = false;

	public void suppressMnemonic() {
		isMnemonicSuppressed = true;
	}

}
